<script setup>
import { onMounted } from "vue";
import Edit from "./components/Edit.vue";
import { ref } from "vue";
import axios from "axios";
// TODO: 列表渲染
// 获取数据 -> 绑定数据
const list = ref([]);

const getData = async () => {
  const res = await axios.get("/list");
  list.value = res.data;
};

onMounted(() => {
  getData();
});

// TODO: 删除功能
// 获取id 调用接口 删除list中的数据
const remove = (id) => {
  console.log(id);
  axios.delete(`/del/${id}`);
  getData();
};
// TODO: 编辑功能
// 点击按钮打开编辑框, 读取之前的数据并添加到当前编辑框中
const editRef = ref(null);
const open = (row) => {
  editRef.value.openEdit(row);
};
</script>

<template>
  <div class="app">
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" @click="open(row)" link>编辑</el-button>
          <el-button type="danger" @click="remove(row.id)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef" @updateData="getData"/>
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
